// index.js
// 获取应用实例
const app = getApp()
let time = ''
Page({
  data: {
    goodInfo: '',
    weatherInfo: '',
    address: '',
    addressInfo: '',
    inputValue: '',
    qCodeImg: undefined,
    userInfo: {},
    img: '',
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },

  onPullDownRefresh: function() {
    this.getImgInfo()
    this.getGoodInfo()
  },

  createQcode(e) {
    this.setData({
      qCodeImg: `https://api.vvhan.com/api/qr?text=${this.data.inputValue}`
    })
  },
  // 事件处理函数
  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },
  showImg(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: [
        e.currentTarget.dataset.url,
      ] // 需要预览的图片http链接列表
    })
  },
  onUnload() {
    clearInterval(time)
  },

  onShareAppMessage: function () {
    return {
      title: '前端历程',
      path: '/pages/personCenter/personCenter',
      imageUrl: '/lib/images/banner1.png'
    }
  },

  onReady() {
    wx.showLoading({
      title: '加载中...',
    })
    this.getImgInfo()
    this.getGoodInfo()
    const app = getApp();
    const addressInfo = app.globalData.addressInfo
    if(!addressInfo) {
      wx.getLocation({
        isHighAccuracy: true, // 开启地图精准定位
        type: 'gcj02', // 地图类型写这个
        success: (res) => {
          const key = 'KVKBZ-QLHC6-CM5SB-EAE37-OUBZS-AZBHK'
          const getAddressUrl = `https://apis.map.qq.com/ws/geocoder/v1/?location=${res.latitude},${res.longitude}&key=${key}&get_poi=1`;
          wx.request({
            url: getAddressUrl,
            success: ops => {
              this.setData({
                address: ops.data.result.ad_info.city,
                addressInfo: ops.data.result.address
              })
  
              this.getWeather()
            }
          })
        },
      })
    } else {
      this.setData({
        address: addressInfo.address,
        addressInfo: addressInfo.addressInfo
      })

      this.getWeather()
    }
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getGoodInfo() {
    wx.request({
      url: `https://api.vvhan.com/api/joke?type=json`,
      success: res => {
        this.setData({
          goodInfo: res.data
        })
        wx.hideLoading()
      }
    })
  },
  getImgInfo() {
    wx.request({
      url: `https://api.vvhan.com/api/acgimg?type=json`,
      success: res => {
        this.setData({
          img: res.data.imgurl
        })
      }
    })
  },
  getWeather() {
    wx.request({
      url: `https://api.vvhan.com/api/weather?city=${this.data.address}`,
      success: res => {
        if(res.data) {
          this.setData({
            weatherInfo: {
              tips: res.data.info.tip,
              wendu: `${res.data.info.low}-${res.data.info.high}`,
              type: res.data.info.type
            }
          })
        }
      }
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
